import 'package:flutter/material.dart';
import 'package:flutter_module/system/constant_config.dart';
import 'package:flutter_module/system/library_map.dart';
import 'package:flutter_module/system/log_util.dart';
import 'package:flutter_module/util/event_bus.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:flutter_module/viewModels/map_control_viewmodel.dart';
import 'package:provider/provider.dart';

class MapControlButtons extends StatefulWidget {
  const MapControlButtons({Key? key}) : super(key: key);

  @override
  _MapControlButtonsState createState() => _MapControlButtonsState();
}

class _MapControlButtonsState extends State<MapControlButtons>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<AlignmentGeometry> _animation;
  late MapControlViewModel _providerRead;

  ///可显示的最大高度 具体的像素
  double bottomSize = 20;
  double opacity = 1.0;

  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 200));

    _animation = Tween<AlignmentGeometry>(
            begin: const FractionalOffset(0.5, 0.5),
            end: const FractionalOffset(0.5, 0.3))
        .animate(_animationController);

    EventBusUtils.getInstance().on<ChangeBottomDrawerTop>().listen((event) {
      _animationController.value = event.animalControllerVale;
      if (_animationController.value <= 0.5) {
        bottomSize = 20 + ConstantConfig.screenSize.height - event.top;
        opacity = 1.0;
      } else {
        opacity = 1.0 - _animationController.value;
      }
    });
  }

  @override
  void didChangeDependencies() {
    _providerRead = Provider.of<MapControlViewModel>(context, listen: false);
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _animation,
        key: const ValueKey("map_buttons"),
        builder: (context, child) {
          return Positioned(
            left: 20,
            bottom: bottomSize,
            child: Opacity(
              opacity: opacity,
              child: child ?? _animationChild(),
            ),
          );
        },
        child: _animationChild());
  }

  Widget _animationChild() {
    return Column(
      children: [
        ///地图放大按钮
        ElevatedButton(
          onPressed: () {
            MapManager.instance.mapController.zoomIn();
          },
          child: const Icon(Icons.add_outlined),
          style: ElevatedButton.styleFrom(
              alignment: Alignment.center,
              padding: const EdgeInsets.all(0),
              minimumSize: const Size(48, 48)),
        ),
        const SizedBox(height: 10.0),

        ///地图缩小按钮
        ElevatedButton(
          onPressed: () {
            MapManager.instance.mapController.zoomOut();
          },
          child: const Icon(Icons.remove_outlined),
          style: ElevatedButton.styleFrom(
              alignment: Alignment.center,
              padding: const EdgeInsets.all(0),
              minimumSize: const Size(48, 48)),
        ),
        const SizedBox(height: 10.0),

        ///回到我的位置按钮
        ElevatedButton(
          onPressed: () {
            _providerRead.setTracking(true);
            if (_providerRead.baiduLocation != null) {
              MapManager.instance.mapController.animateToLatLon(
                  _providerRead.baiduLocation!.latitude!,
                  _providerRead.baiduLocation!.longitude!);
            }
          },
          style: ElevatedButton.styleFrom(
              alignment: Alignment.center,
              padding: const EdgeInsets.all(0),
              minimumSize: const Size(48, 48)),
          child: Icon(Icons.navigation_rounded,
              color: Provider.of<MapControlViewModel>(context).tracking
                  ? Colors.red
                  : Colors.white),
        ),
      ],
    );
  }
}
